package com.bw.mvvm_common.mvvm.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bw.mvvm_common.BaseApplication;
import com.bw.mvvm_common.filed.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;

/**
 * @author KK
 * @description:
 * @date :2021/10/14 14:52
 */
public abstract class BaseFragment<V extends ViewDataBinding,VM extends BaseViewModel> extends Fragment implements IView {

    protected V v;
    protected VM vm;


    @Override
    public View onCreateView( LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        initViewModel();
        bindDataBinding();
        v.setVariable(initVerable(),vm);
        return v.getRoot();
    }

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewModel();
        getLifecycle().addObserver(vm);
        vm.getUiChanage().getHideDialogObservable().observe(this,hideDialogObserver);
        vm.getUiChanage().getFinishActObservable().observe(this,finishObserver);
        vm.getUiChanage().getShowDialogObservable().observe(this,showDialogObserver);
        vm.getUiChanage().getShowMsgObservble().observe(this,showMsgObserver);
        vm.getUiChanage().getStartPageObservable() .observe(this,startPageObserver);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        vm.getUiChanage().getShowDialogObservable().removeObserver(showDialogObserver);
        vm.getUiChanage().getFinishActObservable().removeObserver(finishObserver);
        vm.getUiChanage().getShowDialogObservable().removeObserver(showDialogObserver);
        vm.getUiChanage().getShowMsgObservble().removeObserver(showMsgObserver);
        vm.getUiChanage().getStartPageObservable().removeObserver(startPageObserver);
        getLifecycle().removeObserver(vm);
        v.unbind();
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void showMsg(String msg) {
        Toast.makeText(BaseApplication.getApplication(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void finishPage() {
        getActivity().finish();
    }

    @Override
    public void startPage(Class clazz, Bundle bundle) {
        Intent intent = new Intent(getActivity(),clazz);
        if(bundle!=null){
            intent.putExtra(UIField.PAGEBUNDLE,bundle);
        }
        startActivity(intent);
    }

    @Override
    public void initViewModel() {
        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
        Type[]types = parameterizedType.getActualTypeArguments();
        Class clazz = (Class) types[1];
        vm = (VM) new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(clazz);
    }

    @Override
    public void bindDataBinding() {
        v =  DataBindingUtil.inflate(LayoutInflater.from(getContext()),bindLayout(),null,false);
    }

    private Observer<String> showDialogObserver = new Observer<String>() {
        @Override
        public void onChanged(String o) {
            showDialog();
        }
    };
    private Observer<String>hideDialogObserver = new Observer<String>() {
        @Override
        public void onChanged(String o) {
            hideDialog();
        }
    };
    private Observer<String>finishObserver = new Observer<String>() {
        @Override
        public void onChanged(String o) {
            finishPage();
        }
    };
    private Observer<String>showMsgObserver = new Observer<String>() {
        @Override
        public void onChanged(String o) {
            showMsg(o);
        }
    };
    private Observer<HashMap<String,Object>>startPageObserver = new Observer<HashMap<String, Object>>() {
        @Override
        public void onChanged(HashMap<String, Object> map) {
            Class clazz = (Class) map.get(UIField.PAGECLAZZ);
            Object o =  map.get(UIField.PAGEBUNDLE);
            Bundle bundle = null;
            if(o!=null){
                bundle = (Bundle) o;
            }
            startPage(clazz,bundle);
        }
    };



}
